package com.my.chapter18;


import net.mindview.util.Directory;

import java.io.File;
import java.io.IOException;

public class practice5 {
    public static void main(String[] args) {
        new ProcessFiles2(new ProcessFiles2.Strategy() {
            public void process(File file) {
                System.out.println(file);
            }
        }, ".*\\.java").start(new String[]{".//src//com//my//chapter18"});
    }
}

class ProcessFiles2 {
    public interface Strategy{
        void process(File file);
    }

    private Strategy strategy;
    private String regex;
    public ProcessFiles2(Strategy strategy,
                         String regex) {
        this.strategy = strategy;
        this.regex = regex;
    }

    public void start(String[] args) {
        try {
            if(args.length == 0)
                processDirectoryTree(new File("."));
            else
                for(String arg : args) {
                    File fileArg = new File(arg);
                    if(fileArg.isDirectory())
                        processDirectoryTree(fileArg);
                    else
                    if(arg.matches(regex))
                        strategy.process(fileArg.getCanonicalFile());
                }
        } catch(IOException e) {
            throw new RuntimeException(e);
        }
    }

    public void
    processDirectoryTree(File root) throws IOException {
        for(File file : Directory.walk(
                root.getAbsolutePath(), regex))
            strategy.process(file.getCanonicalFile());//该方法返回同一个由规范路径名字符串表示的文件或目录。
    }

}
